package com.zzyl.intercept.interceptor;

import com.zzyl.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取请求路径
        String requestURI = request.getRequestURI();

        // 判断请求路径是否为登录页面，如果是则放行
        if (requestURI.contains("login")) {
            return true;
        }


        if(requestURI.contains("doc.html")){
            return true;
        }

        // 验证token是否正确，这里假设token验证的方法为validateToken
        String token = request.getHeader("Authorization");

        Claims claims ;
        try {
            claims = JwtUtil.parseJWT("123", token);
            return true;
        }catch (RuntimeException e){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return false;
        }


    }


}